// pages/login/phone/phone.js
const app = getApp()
const Button = wx.getMenuButtonBoundingClientRect()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight,
    Button: Button,
    CDNPATH: app.globalData.CDNPATH,
    phoneNum: '',
    smsCode: '',
    captcha: '',
    captchaImg: '',
    time: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      phoneNum: wx.getStorageSync('phoneNum') || ''
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.changeCaptcha()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  sendCaptcha: function (params) {
    const { phoneNum, captcha } = this.data
    if (!phoneNum) {
      wx.showToast({
        title: `请输入手机号`,
        icon: 'none',
        duration: 2000
      })
    } else if (!captcha) {
      wx.showToast({
        title: `请输入图片验证码`,
        icon: 'none',
        duration: 2000
      })
    } else {
      app.api.login({
        url: '/cpic/sendSmsCode',
        data: {
          phoneNum,
          captcha
        }
      }).then(res => {
        wx.showToast({
          title: `今天剩下${res.data.leaveCount}次获取验证码的机会`,
          icon: 'none',
          duration: 2000
        })
        this.setData({
          time: 59
        })
        let time = 59
        var countdown = setInterval(() => {
          if (time == 0) {
            this.setData({
              time: 0,
            });
            // 取消定时器
            clearInterval(countdown);
          } else {
            this.setData({
              time: time--,
            });
          }
        }, 1000);
      }, () => {
        this.changeCaptcha()
      })
    }
  },
  changeCaptcha() {
    app.api.getArrayBuffer({
      url: '/cpic/captcha',
      data: {
      }
    }).then(res => {
      const img = wx.arrayBufferToBase64(res)
      this.setData({ captchaImg: img })
    })
  },
  login: function () {
    const { phoneNum, smsCode } = this.data
    app.api.login({
      url: '/cpic/smsLogin',
      data: {
        phoneNum,
        smsCode,
      }
    }).then(res => {
      wx.setStorageSync('phoneNum', this.data.phoneNum)
      app.globalData.userInfo = res.data
      wx.setStorageSync("isLogin", true)
      wx.setStorageSync("token", res.data.token)
      wx.setStorageSync("userInfo", JSON.stringify(res.data))
      app.api.post({
        url: 'operation_add',
        data: {
          operationType: 'activity'
        }
      })
      app.api.post({
        url: 'vx_feedback_hasNewMsgTips'
      }).then(res => {
        if (res.hasNewMsg > 0) {
          wx.showTabBarRedDot({
            index: 4
          })
        } else {
          wx.hideTabBarRedDot({
            index: 4,
            fail: res => {
              console.log(res);
            }
          })
        }
      })
      wx.switchTab({
        url: `/pages/learn/index/index`,
      })
    })
  },
  focus(e) {
    const { type } = e.currentTarget.dataset
    this.setData({
      focus: type
    })
  },
  blur() {
    this.setData({
      focus: false
    })
  },
  reBack() {
    wx.navigateBack()
  }
})